﻿using MediatR;
using S.Services.SheepMale.Api.Application.Command.MarketCommand;
using S.Services.SheepMale.Domain.PurchaseSales;
using S.Services.SheepMale.Infrastructure;

namespace S.Services.SheepMale.Api.Application.CommandHandler.MarketCommandHandler
{
	public class MarketAllExamineCommandHandler : IRequestHandler<MarketAllExamineCommand, int>
	{
		private readonly IRepository<EntityMarket> repositoy;

		public MarketAllExamineCommandHandler(IRepository<EntityMarket> repositoy)
		{
			this.repositoy = repositoy;
		}

		public async Task<int> Handle(MarketAllExamineCommand request, CancellationToken cancellationToken)
		{
			string[] result = request.marketId.Split(',');
			List<EntityMarket> list = new List<EntityMarket>();
            foreach (var item in result)
            {
				var type = await repositoy.GetAsync(int.Parse(item));
				if (type.AuditState==2)
				{
					return -1;
				}
				type.AuditState = 2;
				type.ExamineDate = DateTime.Now;
				type.ExamineBy = "阿星";
				list.Add(type);
				
            }
            foreach (var item in list)
            {
				return await repositoy.UpdateAsync(item);
			}
            return 1;
        }
	}
}
